from random import randint
from time import sleep

from selenium import  webdriver
from selenium.webdriver.support.select import Select

driver = webdriver.Firefox()

driver.get('http://localhost/jxc/index.asp')

ipts = driver.find_elements_by_tag_name('input')

ipts[0].send_keys('admin')
ipts[1].send_keys('admin')
ipts[2].click()

driver.switch_to.frame('left')

sysset_css='.menuall > tbody:nth-child(1) > tr:nth-child(13) > td:nth-child(1) > img:nth-child(1)'
driver.find_element_by_css_selector(sysset_css).click()

danwei_css='#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(7) > td:nth-child(1)'
sleep(2)
driver.find_element_by_css_selector(danwei_css).click()

driver.switch_to.default_content()
driver.switch_to.frame('right')

#删除
driver.find_element_by_name('chkall').click()
shanchu_css='input.button:nth-child(2)'
driver.find_element_by_css_selector(shanchu_css).click()
sleep(2)
driver.switch_to.alert.accept()

#新添加单位
bt_css='input.button:nth-child(1)'
driver.find_element_by_css_selector(bt_css).click()

driver.find_element_by_name('danwei').send_keys('克')
driver.find_element_by_name('submit').click()
sleep(2)
driver.switch_to.alert.accept()
sleep(3)

#产品大类管理
driver.switch_to.default_content()
driver.switch_to.frame('left')
dalei_css='#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(5) > td:nth-child(1)'
driver.find_element_by_css_selector(dalei_css).click()

driver.switch_to.default_content()
driver.switch_to.frame('right')

#bt1_css='input.button:nth-child(1)'
driver.find_element_by_css_selector(bt_css).click()

driver.find_element_by_name('bigclass').send_keys('食物')
driver.find_element_by_name('submit').click()
sleep(2)
driver.switch_to.alert.accept()
sleep(3)

#产品小类管理
driver.switch_to.default_content()
driver.switch_to.frame('left')
xiaolei_css='#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(6) > td:nth-child(1)'
driver.find_element_by_css_selector(xiaolei_css).click()

driver.switch_to.default_content()
driver.switch_to.frame('right')

#bt_css='input.button:nth-child(1)'
driver.find_element_by_css_selector(bt_css).click()

#随机生成
sel_list=Select(driver.find_element_by_name('bigclass'))

lst_count=len(sel_list.options)
rand=randint(0,lst_count-1)
sel_list.select_by_index(rand)
sleep(3)

#添加小类
driver.find_element_by_name('smallclass').send_keys('饺子')
driver.find_element_by_name('submit').click()
driver.switch_to.alert.accept()
sleep(3)

driver.switch_to.default_content()

driver.switch_to.frame('topFrame')
driver.find_element_by_link_text('安全退出').click()
sleep(2)

driver.switch_to.alert.accept()

sleep(2)
driver.quit()











